package com.smart.village.big.screen.controller.micro;

import com.smart.village.big.screen.domain.AjaxResult;
import com.smart.village.big.screen.service.NewsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.logging.Logger;

import static com.smart.village.big.screen.domain.AjaxResult.error;
import static com.smart.village.big.screen.domain.AjaxResult.success;


/**
 * 小程序-新闻类
 */
@Tag(name = "小程序-新闻类数据接口")
@RestController
@RequestMapping("/micro/news")
public class MicroNewsController {

    Logger logger = Logger.getLogger(MicroNewsController.class.getName());

    @Autowired
    private NewsService newsService;
    /**
     * 小程序 通知公告
     *
     * @param partyOrganizationSerialNumber
     * @return
     */
    @Operation(summary = "小程序 通知公告 最新的10条数据")
    @GetMapping("/getNoticePublish")
    @ResponseBody

    public AjaxResult getNoticePublish(
            @RequestParam @Parameter(description = "终端") String NewsTerminal,
            @RequestParam @Parameter(description = "组织id") String partyOrganizationSerialNumber,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize
    ) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(newsService.getNoticePublish(partyOrganizationSerialNumber, pageNum, pageSize,1,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 留言
     *
     * @param phone
     * @return
     */
    @Operation(summary = "小程序 留言")
    @GetMapping("/getLeaveWord")
    @ResponseBody

    public AjaxResult getLeaveWord(
            @RequestParam @Parameter(description = "用户手机号") String phone,
            @RequestParam(required = false) @Parameter(description = "评论回复状态 我的留言,已回复，已评价") String replyStatus,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize
    ) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(newsService.getLeaveWord(phone, replyStatus, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }
    /**
     * 小程序 留言-详情
     *
     * @param messageSerialNumber
     * @return
     */
    @Operation(summary = "小程序 留言-详情")
    @GetMapping("/getLeaveWordDetail")
    @ResponseBody

    public AjaxResult getLeaveWordDetail(
        String messageSerialNumber
    ) {
        try {

            return success(newsService.getLeaveWordDetail(messageSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 根据新闻主键 获取详情
     *
     * @param newsSerialNumber
     * @return
     */
    @Operation(summary = "小程序 新闻列表 根据新闻序列号 获取详情")
    @GetMapping("/selectByPrimaryKey/{newsSerialNumber}")
    @ResponseBody
    public AjaxResult selectByPrimaryKey(@PathVariable("newsSerialNumber") @Parameter(description = "新闻序列号") String newsSerialNumber) {
        try {
            return success(newsService.selectByPrimaryKey(newsSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 新闻列表
     * 根据不同的标签 每个标签获取 最新的10条数据
     *
     * @param newsTagSerialNumber
     * @return
     */
    @Operation(summary = "小程序 新闻列表 根据不同的标签 每个标签获取 最新的10条数据")
    @GetMapping("/getWorkDynamicList")
    @ResponseBody

    public AjaxResult getWorkDynamicList(
            @RequestParam @Parameter(description = "终端") String NewsTerminal,
            @RequestParam(required = false) @Parameter(description = "标签序列号") String newsTagSerialNumber,
            @RequestParam(required = false) @Parameter(description = "搜索") String search,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize
    ) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(newsService.getWorkDynamicList(newsTagSerialNumber, pageNum, pageSize,search,1,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    /**
     * 小程序 新闻列表
     * 根据不同的分类 每个分类获取 最新的10条数据
     *
     * @return
     */
    @Operation(summary = "小程序 新闻列表 根据不同的分类 每个分类获取 最新的10条数据")
    @GetMapping("/getList")
    @ResponseBody
    public AjaxResult getList(
            @RequestParam @Parameter(description = "终端") String NewsTerminal,
            @RequestParam(required = false) @Parameter(description = "主分类序列号") String newsCategorySerialNumber,
            @RequestParam(required = false) @Parameter(description = "搜索") String search,
            @RequestParam(required = false) @Parameter(description = "权限字段") String partyOrganizationActivitySerialNumber,

            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(newsService.getList(newsCategorySerialNumber, partyOrganizationActivitySerialNumber,pageNum, pageSize,search,1,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }
    /**
     * 小程序 综合搜索，查询新闻 视频 电子书
     * 最新的10条数据
     *
     * @return
     */
    @Operation(summary = "小程序 综合搜索，查询新闻 视频 电子书")
    @GetMapping("/getAllListSearch")
    @ResponseBody
    public AjaxResult getAllListSearch(
            @RequestParam @Parameter(description = "终端") String NewsTerminal,
            @RequestParam(required = false) @Parameter(description = "搜索") String search,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(newsService.getAllListSearch( pageNum, pageSize,search,1,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }
    /**
     * 小程序 综合搜索，详情
     *
     * @return
     */
    @Operation(summary = "小程序 综合搜索，详情")
    @GetMapping("/getDetailSearch")
    @ResponseBody
    public AjaxResult getDetailSearch(
            @RequestParam @Parameter(description = "id") String id,
            @RequestParam @Parameter(description = "type") String searchType) {
        try {

            return success(newsService.getDetailSearch( id, searchType));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }
    /**
     * 小程序 双联系双报到 新闻列表
     * 根据不同的分类 每个分类获取 最新的10条数据
     *
     * @return
     */
    @Operation(summary = "小程序 双联系双报到 新闻列表")
    @GetMapping("/getTwoContactTwoReportNewsList")
    @ResponseBody
    public AjaxResult getTwoContactTwoReportNewsList(
            @RequestParam @Parameter(description = "终端") String NewsTerminal,
            @RequestParam(required = false) @Parameter(description = "默认当前月份")String formatDate,
            @RequestParam(required = false) @Parameter(description = "搜索") String search,
            @RequestParam(required = false) @Parameter(description = "组织id")String partyOrganizationSerialNumber,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(newsService.getMicroTwoContactTwoReportNewsList(formatDate,partyOrganizationSerialNumber,"支部", pageNum, pageSize,search,1,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    /**
     * 小程序 所有党建资讯分类列表
     *
     * @return
     */
    @Operation(summary = "小程序 所有党建资讯分类列表")
    @GetMapping("/getPartyNewsCategoryList")
    @ResponseBody
    public AjaxResult getPartyNewsCategoryList() {
        try {
            return success(newsService.getPartyNewsCategoryList());
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    /**
     * 小程序 党建资讯新闻分类列表
     *
     * @return
     */
    @Operation(summary = "小程序 所有新闻分类列表")
    @GetMapping("/getNewsCategoryList")
    @ResponseBody
    public AjaxResult getNewsCategoryList(
            @RequestParam(required = false) @Parameter(description = "终端") String NewsTerminal) {
        try {
            return success(newsService.getNewsCategoryList(NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 所有新闻标签列表
     *
     * @return
     */
    @Operation(summary = "小程序 所有新闻标签列表")
    @GetMapping("/getNewsTagsList")
    @ResponseBody
    public AjaxResult getNewsTagsList() {
        try {
            return success(newsService.getNewsTagsList());
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 搜索里面的四个列表
     *
     * @return
     */
    @Operation(summary = "小程序 搜索里面的四个列表")
    @GetMapping("/searchContentList")
    @ResponseBody
    public AjaxResult searchContentList(
            @RequestParam @Parameter(description = "终端") String NewsTerminal){
        try {
            return success(newsService.searchContentList(1,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    /**
     * 小程序 研学阵地-全部讨论
     *
     * @return
     */
    @Operation(summary = "小程序 研学阵地-全部讨论")
    @GetMapping("/study/allDiscuss")
    @ResponseBody
    public AjaxResult getStudyAllDiscuss(
            @RequestParam @Parameter(description = "终端") String NewsTerminal,
            @RequestParam(required = false) @Parameter(description = "默认当前月份")String formatDate,
            @RequestParam(required = false) @Parameter(description = "搜索") String search,
            @RequestParam(required = false, defaultValue = "") @Parameter(description = "组织id")String partyOrganizationSerialNumber,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        {
            try {
                if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                    pageNum = 1;
                }
                if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                    pageSize = 10;
                }
                return success(
                        newsService.
                                getStudyAllDiscuss(formatDate, partyOrganizationSerialNumber,
                                        "支部", pageNum, pageSize, search,1,NewsTerminal));
            } catch (Exception e) {
                e.printStackTrace();
                logger.severe(e.getMessage());
                return error("程序错误请联系系统管理员");
            }
        }
    }

    /**
     * 小程序 研学阵地-获取文章内的评论列表
     *
     * @return
     */
    @Operation(summary = "小程序 研学阵地-获取文章内的评论列表")
    @GetMapping("/study/discuss/detail/comments")
    @ResponseBody
    public AjaxResult getDiscussDetailComments(
            @RequestParam @Parameter(description = "文章id")String commentObjectSerialNumber,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        {
            try {
                if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                    pageNum = 1;
                }
                if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                    pageSize = 10;
                }
                return success(
                        newsService.
                                getDiscussDetailComments(commentObjectSerialNumber, "0",pageNum, pageSize));
            } catch (Exception e) {
                e.printStackTrace();
                logger.severe(e.getMessage());
                return error("程序错误请联系系统管理员");
            }
        }
    }

    /**
     * 小程序 研学阵地-我的讨论
     *
     * @return
     */
    @Operation(summary = "小程序 研学阵地-我的讨论")
    @GetMapping("/study/myDiscuss")
    @ResponseBody
    public AjaxResult getStudyMyDiscuss(
            @RequestParam @Parameter(description = "用户手机号")String phone,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        {
            try {
                if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                    pageNum = 1;
                }
                if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                    pageSize = 10;
                }
                return success(
                        newsService.
                                getStudyMyDiscuss(phone,"0", pageNum, pageSize));
            } catch (Exception e) {
                e.printStackTrace();
                logger.severe(e.getMessage());
                return error("程序错误请联系系统管理员");
            }
        }
    }

    @Operation(summary = "小程序 我的-党员事务-驻村工作队-工作报导")
    @GetMapping("/workTeamLog/newsList")
    @ResponseBody
    public AjaxResult getWorkTeamLogNewsList(
            @RequestParam @Parameter(description = "终端") String NewsTerminal,
            @RequestParam @Parameter(description = "工作队id") String workTeamSerialNumber,
            @RequestParam(required = false) @Parameter(description = "用户id") String phone,
            @RequestParam(required = false) @Parameter(description = "默认当前月份")String formatDate,
            @RequestParam(required = false) @Parameter(description = "默认空")String status,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(newsService.getWorkTeamLogNewsList(workTeamSerialNumber, phone,formatDate,status,pageNum, pageSize,1,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }
    @Operation(summary = "首页轮播-智慧党建")
    @Parameters({
    })
    @GetMapping("getCarouselConfig")
    @ResponseBody
    public AjaxResult getCarouselConfig(
    ) {
        try {

            return success(newsService
                    .getCarouselConfig()
            );
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe("首页轮播-智慧党建失败: " + e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }
}
